/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package control;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cs4280.dao.*;
import cs4280.bean.*;
import java.util.ArrayList;

/**
 *
 * @author PangPei
 */
public class MessageControl extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet MessageControl</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet MessageControl at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    }

public void retrieveMessages(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        MessageDAO messageDAO = new MessageDAO();
        ArrayList<Message> messages=(ArrayList<Message>)messageDAO.getMessages();
        request.setAttribute("messages", messages);
    }

 public String gettime() {
    String datestr =  "" ;
    try {
    java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd- HH:mm") ;
    java.util.Date date = new java.util.Date() ;
    datestr = df.format(new java.util.Date()) ;
    }
    catch (Exception ex) {

    }

    return datestr ;
  }

public void doAddMessage(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        Message message = new Message();
        MessageDAO messageDAO = new MessageDAO();
        message.setEmail(request.getParameter("email"));
        String name=request.getParameter("username");
        message.setName(name);
        
        message.setTime(gettime());
        message.setContent(request.getParameter("msgcontent"));
        switch(messageDAO.addMessage(message)) {
            case OK:
                request.setAttribute("msg", "Leave a message successful!");
                break;
            case FAIL:
                request.setAttribute("msg", "Sorry something failed, please try to leave a message agian");
                break;
        }
        ArrayList<Message> messages=(ArrayList<Message>)messageDAO.getMessages();
        request.setAttribute("messages", messages);
    }

public void doDeleteMessage(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        MessageDAO messageDAO = new MessageDAO();
        int messageid=Integer.parseInt(request.getParameter("messageid"));
        switch(messageDAO.deleteMessage(messageid)) {
            case OK:
                request.setAttribute("msg", "Delete a message successful!");
                break;
            case FAIL:
                request.setAttribute("msg", "Sorry something failed, please try to delete the message agian");
                break;
        }
        ArrayList<Message> messages=(ArrayList<Message>)messageDAO.getMessages();
        request.setAttribute("messages", messages);
    }    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
